package com.minibobbo.utilities.camerautilities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;

public class CameraUtil {
	private OrthographicCamera c;

	public CameraUtil(OrthographicCamera c) {
		this.c = c;

	}

	private boolean shakeCamera = false;
	private float shakeDuration;
	private float totalShakeDuration;
	private float strength;
	private Vector2 shakeOrigin;

	/**
	 * Shake the camera. The camera moves randomly around.
	 * 
	 * @param duration
	 *            How long it should shake
	 * @param strength
	 *            How far in world units it should move at maximum.
	 */
	public void shakeCamera(float duration, float strength) {
		shakeCamera = true;
		this.strength = strength;
		shakeDuration = 0;
		totalShakeDuration = duration;
		shakeOrigin = new Vector2();
	}

	private boolean transitionCamera = false;;
	private float transDuration;
	private float totalTransDuration;
	private Vector2 origin;
	private Vector2 destination;

	/**
	 * Moves the camera to a location in a certain amount of time.
	 * 
	 * @param x
	 *            X location
	 * @param y
	 *            Y location
	 * @param time
	 *            How long it should take to move there. This must be positive.
	 */
	public void transitionTo(float x, float y, float time) {
		if (time > 0) {
			transitionCamera = true;
			origin = new Vector2(c.position.x, c.position.y);
			destination = new Vector2(x, y).sub(origin);
			totalTransDuration = time;
			transDuration = 0;
		}
	}

	/**
	 * Cancels the current camera transition
	 */
	public void cancelTransition() {
		transitionCamera = false;
	}

	/**
	 * Updates the camera based on the commands given. This should be run in the
	 * render loop.
	 * 
	 * @param delta
	 *            The amount of time that has passed since the last update.
	 *            Normally should be passed Gdx.graphics.getDeltaTime();
	 */
	public void update(float delta) {
		// Transition
		if (transitionCamera) {
			transDuration += delta;
			c.position.x = origin.x + destination.x
					* (transDuration / totalTransDuration);
			c.position.y = origin.y + destination.y
					* (transDuration / totalTransDuration);
			if (transDuration >= totalTransDuration) {
				transitionCamera = false;
				c.position.x = origin.x + destination.x;
				c.position.y = origin.y + destination.y;

			}
			c.update();

		}
		
		//If the camera is shaking.
		if(shakeCamera) {
			shakeDuration += delta;
			shakeOrigin.set(c.position.x, c.position.y);
			float tempStrength = strength * (1-(shakeDuration/totalShakeDuration));
			Gdx.app.log("Shaking", tempStrength + "");

			c.position.x = c.position.x + (MathUtils.random(-tempStrength,tempStrength));
			c.position.y = c.position.y + (MathUtils.random(-tempStrength,tempStrength));
			c.update();
			c.position.x = shakeOrigin.x;
			c.position.y = shakeOrigin.y;
			if(shakeDuration >= totalShakeDuration) {
				shakeCamera = false;
				c.update();
			}
		}
		
	}
	
	

}
